using System;
using System.Collections.Generic;
using System.Text;
using Adoor.Object.Entity;
using Adoor.Object.Query;

using System.Drawing.Design;
using System.ComponentModel.Design;
using System.ComponentModel;  

namespace Adoor.Object.Domain
{
    /// <summary>
    /// This <see cref="IObjectResolver"/> implementation perform lookups on a specific LinkedDomainCollection .
    /// </summary>
    [Serializable,System.ComponentModel.ToolboxItem(true), System.ComponentModel.DesignTimeVisible(true)]    
    public class LinkedDomainObjectResolver : ObjectResolverBase
    {
        private LinkedDomainCollection linkedDomainCollection;

        public LinkedDomainObjectResolver() : this(new LinkedDomainCollection())
        {
              
        }

        public LinkedDomainObjectResolver(LinkedDomainCollection linkedDomainCollection)
        {
            this.linkedDomainCollection = linkedDomainCollection;
            this.linkedDomainCollection.objectResolver = this;
        }

        //[Browsable(false)]
        [Category("Behavior"),
        DescriptionAttribute("Link to a Domain Collection"),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        TypeConverter(typeof(ExpandableObjectConverter ))]
        public LinkedDomainCollection LinkedDomainCollection
        {
            get { return this.linkedDomainCollection; }
            set { this.linkedDomainCollection = value; }
        }

        #region IObjectResolver Members

        private List<ObjectDomain> cacheManagedDomains;
        public override ICollection<ObjectDomain> ManagedDomains
        {
            get
            {
                if (cacheManagedDomains != null)
                    return cacheManagedDomains;

                cacheManagedDomains = new List<ObjectDomain>();
                foreach (LinkedDomainGroup dg in linkedDomainCollection)
                    cacheManagedDomains.AddRange(dg.FlattenedDomains);

                return cacheManagedDomains;
            }
        }

        
        #endregion
    }
}
